/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

package com.tristia.entity;

import java.util.Formatter;
import java.util.Set;
import java.util.TreeSet;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@Entity
@Table(name = "book")
@NamedQueries( { @NamedQuery(name = "Book.findAll", query = "SELECT p FROM Book p"),
	@NamedQuery(name = "Book.findBookByISBN", query = "SELECT p FROM Book p where p.isbn=:isbn"),
	@NamedQuery(name = "Book.findBook", query = "SELECT p FROM Book p where p.title=:title and p.author=:author")
	})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "book", propOrder = {
	"isbn",
	"date",
	"publisher",
	"subject",
    "intro",
    "avgRating",
    "reviews",
    "states",
    "bookstores"
})
@XmlRootElement
public class Book extends Item {

	private Long isbn;
	private String date;
	private String publisher;
	private String subject;
    private String intro;
    private Float avgRating;
    
//	@XmlElementWrapper(name = "reviews")
	@XmlElement(name = "review")
    @OneToMany(mappedBy="book", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private Set<Review> reviews = new TreeSet<Review>();
	
//	@XmlElementWrapper(name = "reviews")
	@XmlElement(name = "state")
    @OneToMany(mappedBy="book", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
    private Set<State> states = new TreeSet<State>();
	
	@ManyToMany(fetch = FetchType.EAGER) 
    @JoinTable(
            name="book_bookstore",
            joinColumns = { @JoinColumn( name="book_id") },
            inverseJoinColumns = @JoinColumn( name="bookstore_id")
    )   
    private Set<Bookstore> bookstores = new TreeSet<Bookstore>();
	
    public Book() {
    }

    public Book(final String title, final String author) {
        super(title, author);
    }

	public Long getIsbn() {
		return isbn;
	}

	public void setIsbn(Long isbn) {
		this.isbn = isbn;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getPublisher() {
		return publisher;
	}

	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getIntro() {
		return intro;
	}

	public void setIntro(String intro) {
		this.intro = intro;
	}

	public Float getAvgRating() {
		return avgRating;
	}

	public void setAvgRating(Float avgRating) {
		this.avgRating = avgRating;
	}

	public Set<Review> getReviews() {
		return reviews;
	}

	public void setReviews(Set<Review> reviews) {
		this.reviews = reviews;
	}

	public Set<State> getStates() {
		return states;
	}

	public void setStates(Set<State> states) {
		this.states = states;
	}

	public Set<Bookstore> getBookstores() {
		return bookstores;
	}

	public void setBookstores(Set<Bookstore> bookstores) {
		this.bookstores = bookstores;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((intro == null) ? 0 : intro.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Book other = (Book) obj;
		if (intro == null) {
			if (other.intro != null)
				return false;
		} else if (!intro.equals(other.intro))
			return false;
		return true;
	}

	@Override
	public String toString() {
        return (new Formatter()).format(
                "{\"id\":\"%d\",\"title\":\"%s\",\"author\":\"%s\",\"intro\":%s}",
                super.getId(), super.getTitle(), super.getAuthor(), this.intro).toString();
	}

}
